﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emDocFileDetail : common.control.baseUserControl
    {
        private BindingSource myDataSource = null;
        public emDocFileDetail()
        {
            InitializeComponent();
            fileIdEd.BackColor = common.settings.sysColorDisableBG;
            fileNameEd.BackColor = common.settings.sysColorDisableBG;
            sizeEd.BackColor = common.settings.sysColorDisableBG;
            SetMaxLength();

            openFileDialog.Filter = common.system.MakeFileDialogFilter_ImageAndDocument();
        }

        public void SetDataSource(System.Windows.Forms.BindingSource dataSrc)
        {
            this.myDataSource = dataSrc;
            data.baseDS.emFileDataTable tbl = (data.baseDS.emFileDataTable)dataSrc.DataSource;

            this.fileIdEd.DataBindings.Clear();
            this.fileIdEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.fileIdColumn.ColumnName, true));

            this.fileNameEd.DataBindings.Clear();
            this.fileNameEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.fileNameColumn.ColumnName, true));

            this.fileDescEd.DataBindings.Clear();
            this.fileDescEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.descriptionColumn.ColumnName, true));

            this.sizeEd.DataBindings.Clear();
            this.sizeEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.sizeKBColumn.ColumnName, true));
        }
        private void SetMaxLength()
        {
            data.baseDS.emEducationDataTable tbl = new data.baseDS.emEducationDataTable();
            fileDescEd.MaxLength = tbl.notesColumn.MaxLength;
        }
        public virtual void Init() 
        {
        }

        public virtual bool CheckData()
        {
            if (this.myDataSource.Current == null) return true;
            bool retVal = true;
            ClearNotifyError();
            if (fileNameEd.Text.Trim() == "") 
            {
                NotifyError(fileNameLbl);
                retVal = false;
            }
            if (fileDescEd.Text.Trim() == "")
            {
                NotifyError(fileNameLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            if (myDataSource.Current == null) state = true; 
            fileDescEd.ReadOnly = state;
            openBtn.Enabled = !state;
            scanBtn.Enabled = !state;
            uploadBtn.Enabled = !state;
            downLoadbtn.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            selectFileNameEd.Focus();
        }

        private void uploadBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!common.fileFuncs.FileExist(selectFileNameEd.Text))
                {
                    common.system.ShowErrorMessage("Không có file.");
                    selectFileNameEd.Focus();
                }
                if (fileDescEd.Text.Trim() == "") fileDescEd.Text = common.fileFuncs.FileNameOnly(selectFileNameEd.Text);
                this.sizeEd.Value = (int)(common.fileFuncs.FileSize(this.selectFileNameEd.Text) / 1024);
                this.fileNameEd.Text = common.fileFuncs.UploadFile(this.selectFileNameEd.Text);
                selectFileNameEd.Text = "";
                myDataSource.EndEdit();
                ErrorHandler(sender, null);
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void downLoadbtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (fileIdEd.Text.Trim() == "")
                {
                    common.system.ShowErrorMessage("Không có file."); return;
                }
                string tmp = common.fileFuncs.DownloadFile(fileNameEd.Text);
                common.system.ShowMessage("Đã lưu vào tệp " + tmp);
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.openFileDialog.ShowDialog() != DialogResult.OK) return;
                this.selectFileNameEd.Text = this.openFileDialog.FileName;
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void scanBtn_Click(object sender, EventArgs e)
        {
            try
            {
                bool retVal = true;
                string fileName = common.images.ScanFile(out retVal);
                if (retVal && fileName != null) selectFileNameEd.Text = fileName; 
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
    }
}
